package com.feige.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feige.constant.Constants;
import com.feige.pojo.ExerciseTitle;
import com.feige.result.AjaxResult;
import com.feige.service.ExerciseTitleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 习题标题 前端控制器
 * </p>
 *
 * @author feige
 * @since 2021-04-01
 */
@Api(tags = "习题标题前端控制器")
@RestController
@RequestMapping("/exercise/exercise-title")
public class ExerciseTitleController {

    @Resource
    private ExerciseTitleService exerciseTitleService;



    @ApiOperation(value = "保存习题标题")
    @PostMapping("/save")
    public AjaxResult save(@RequestBody ExerciseTitle exerciseTitle){
        boolean save = exerciseTitleService.save(exerciseTitle);
        if (save){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "教师分页查询习题标题")
    @GetMapping("/{pageNum}/{id}")
    public AjaxResult getTeaExerciseTitleByCourseId(
            @PathVariable Integer pageNum,
            @PathVariable String id){
        Page<ExerciseTitle> page = new Page<>(pageNum, Constants.PAGE_SIZE * 2);
        QueryWrapper<ExerciseTitle> wrapper = new QueryWrapper<>();
        wrapper
                .select("id","title","begin_Time","end_Time")
                .eq("course_id",id)
                .orderByDesc("gmt_create");
        exerciseTitleService.page(page,wrapper);
        AjaxResult success = AjaxResult.success(page.getRecords());
        success.put("total",page.getTotal());
        return success;
    }


    @ApiOperation(value = "学生分页查询习题标题")
    @GetMapping("/stu/{pageNum}/{id}")
    public AjaxResult getStuExerciseTitleByCourseId(
            @PathVariable Integer pageNum,
            @PathVariable String id,
            @RequestHeader("token") String token
    ){
        return exerciseTitleService.getStuExerciseTitleByCourseId(pageNum,id,token);
    }

}

